package org.vetc.mgr.adapter.viewholder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import org.itzheng.and.recyclerview.adapter.BaseRecyclerLoadHintAdapter
import org.itzheng.itplayer.R
import org.itzheng.itplayer.adapter.viewholder.ViewHolder

/**
 * Title:<br></br>
 * Description: <br></br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2020-7-30.
 */
abstract class VHKotlinLoadMoreAdapter : BaseRecyclerLoadHintAdapter<ViewHolder>() {
    override fun onCreateRealViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        return ViewHolder(onCreateView(parent, viewType))
    }

    protected open fun inflate(
        parent: ViewGroup,
        @LayoutRes resource: Int
    ): View {
        return LayoutInflater.from(parent.context)
            .inflate(resource, parent, false)
    }

    protected val mItems = mutableListOf<Any>()
    override fun getItemCount(): Int {
//        return mItems?.size ?: 0
        return BaseRecyclerLoadHintAdapter.getItemCount(mItems, true)
    }

    /**
     * 重置
     */
    fun <T> submitList(list: List<T>?) {
        mItems.clear()
        //清空数据然后添加
        if (list != null) {
            addItems(list)
        }

    }

    /**
     * 添加更多
     */
    fun <T> addItems(list: List<T>?) {
        if (list != null) {
            for (item in list) {
                mItems.add(item!!)
            }
            notifyDataSetChanged()
        }

    }

    /**
     * 获取数据，如果没有的话，返回空，因为使用加载更多，目前有bug
     */
    fun getItem(position: Int): Any? {
        val temp = mItems
        if (temp.size <= position) {
            return null
        }
        return temp.get(position)
    }

//    override fun onCreateContentLoadingView(parent: ViewGroup): View? {
//        return inflate(parent, R.layout.adapter_loading_view_content_loading)
//    }
//
//    override fun onCreateContentEmptyView(parent: ViewGroup): View? {
//        return inflate(parent, R.layout.adapter_loading_view_content_empty)
//    }
//
//    override fun onCreateContentErrorView(parent: ViewGroup): View? {
//        return inflate(parent, R.layout.adapter_loading_view_content_error)
//    }
//
//    override fun onCreateEndFinishView(parent: ViewGroup): View? {
//        return inflate(parent, R.layout.adapter_loading_view_end_finish)
//    }
//
//    override fun onCreateEndErrorView(parent: ViewGroup): View? {
//        return inflate(parent, R.layout.adapter_loading_view_end_error)
//    }
//
//    override fun onCreateEndLoadingView(parent: ViewGroup): View? {
//        return inflate(parent, R.layout.adapter_loading_view_end_loading)
//    }
}